package com.pioneer.swing.model;

import com.pioneer.dao.ManifestDAO;
import com.pioneer.dao.DataAccessException;
import com.pioneer.beans.ContainerContent;
import com.pioneer.beans.Container;
import com.pioneer.beans.Key;
import com.pioneer.beans.ContainerKey;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;
import java.util.List;
import java.util.Iterator;

/**
 * Title:        Pioneer Metals Application
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class ChinaContainerEntryTable extends ContainerEntryTable {

	private ManifestDAO dao;
	private String containerId;
	private String jobNum;

	// column numbers
	public static final int DESCRIPTION=0;
	public static final int PACKING=1;
	public static final int FOB_C_NUM=2;
	public static final int WEIGHT=3;
	public static final int COST=4;
	public static final int FOB_AMOUNT=5;

    public ChinaContainerEntryTable(ManifestDAO dao,Key key) {
		super(dao,key);
		System.out.println("Entering containers for China");
	}

	public void setValueAt(Object aValue,
                       int row,
                       int column) {			
			if ((column==FOB_C_NUM)||
				(column==WEIGHT)||
				(column==COST)) {
				try {					
					//if (column==COST) {
					//	aValue=new Float(Float.parseFloat(aValue.toString())*1.02F);						
					//}
					//else{
					    aValue=new Float(aValue.toString());
					//}
				}
				catch (NumberFormatException ex) {
					aValue=new Float(0);
				}
			}
			super.setValueAt(aValue,row,column);			
			switch(column){
			case FOB_C_NUM:
				updateCost(row);
				updateFobAmount(row);
				break;
			case WEIGHT:
				updateFobAmount(row);
				break;
			case COST:
				updateFobAmount(row);
				break;
			}			
	}
	
	private void updateFobAmount(int row) {
		Object costObj=getValueAt(row,COST);
		Object weightObj=getValueAt(row,WEIGHT);
		if ((costObj!=null)&&(weightObj!=null)) {
			try {
				float cost=((Float)costObj).floatValue();
				float fob=cost*((Float)weightObj).floatValue();
				super.setValueAt(new Float(fob),row,FOB_AMOUNT);
				//super.setValueAt(new Float(cost),row,COST);
			}
			catch (Exception ex) {
				//ex.printStackTrace(System.err);
				super.setValueAt(new Float(0),row,FOB_AMOUNT);
				//super.setValueAt(new Float(0),row,COST);
			}
		}
	}
	

	private void updateCost(int row) {
		Object cNumObj=getValueAt(row,FOB_C_NUM);
		if (cNumObj!=null) {
			try {
				float cost=((Float)cNumObj).floatValue()*1.02F;
				super.setValueAt(new Float(cost),row,COST);
			}
			catch (Exception ex) {
				//ex.printStackTrace(System.err);
				super.setValueAt(new Float(0),row,COST);
			}
		}
	}


	public boolean isCellEditable(int rowIndex,
                              int columnIndex) {
		if (columnIndex==FOB_AMOUNT)	return false;
		else return true;
	}

    public static void main(String[] args) {
        //ContainerEntryTable allManifestsTable1 = new ContainerEntryTable();
    }
}